using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Voodoo.Pattern;

public class GameplayAudio : MonoBehaviour, ISwichSound
{
	private List<AudioSource> _audioSources = new List<AudioSource>();

	private List<AudioClip> _audioClips = new List<AudioClip>();

	[SerializeField]
	private AudioClip[] _digSounds;

	[SerializeField]
	private AudioClip[] _scratchSounds;

	[SerializeField]
	private AudioClip _coinCollect;

	[SerializeField]
	private AudioClip _upgrade;

	[SerializeField]
	private AudioSource _machineAudioSource;

	[SerializeField]
	private MoveDirection _joystick;

	private int _currentDigSounds;

	private int _currentScratchSounds;

	private int _currentCoinSounds;

	private float _scratchSoundTimer;

	private float _digSoundTimer;

	private float _coinTimer;

	private bool _canPlay;

	private void Start()
	{
		for (int i = 0; i < 5; i++)
		{
			AudioSource item = base.gameObject.AddComponent<AudioSource>();
			AudioSourceSetting(item);
			_audioSources.Add(item);
		}
	}

	private void Awake()
	{
		if ((bool)SingletonMB<GameManager>.Instance)
		{
			SingletonMB<GameManager>.Instance.GameplayAudio = this;
		}
		if ((bool)CityBuildingManager.Instance)
		{
			CityBuildingManager.Instance.GameplayAudio = this;
		}
	}

	internal void ReplaceJoystick(MoveDirection joystick)
	{
		_joystick = joystick;
	}

	private void FixedUpdate()
	{
		_scratchSoundTimer -= Time.deltaTime;
		_digSoundTimer -= Time.deltaTime;
		_coinTimer -= Time.deltaTime;
		if (_joystick != null)
		{
			_machineAudioSource.pitch = Vector2.Distance(Vector2.zero, _joystick.Direction) + Random.Range(-0.1f, 0.1f);
		}
	}

	public void PlaySound(GameplaySoundType soundType)
	{
		if (_canPlay)
		{
			if (soundType == GameplaySoundType.DigSound && _currentDigSounds < 5 && _digSoundTimer < 0f)
			{
				DigSound();
			}
			if (soundType == GameplaySoundType.Scratch && _currentScratchSounds < 3 && _scratchSoundTimer < 0f)
			{
				ScratchSound();
			}
			if (soundType == GameplaySoundType.Coin && _currentCoinSounds < 5 && _coinTimer < 0f)
			{
				CoinCollectedSound();
			}
			if (soundType == GameplaySoundType.Upgrade && _currentCoinSounds < 5 && _coinTimer < 0f)
			{
				UpgradeShownSound();
			}
		}
	}

	private AudioSource audioSource()
	{
		for (int i = 0; i < _audioSources.Count; i++)
		{
			if (!_audioSources[i].isPlaying)
			{
				return _audioSources[i];
			}
		}
		AudioSource result = base.gameObject.AddComponent<AudioSource>();
		AudioSourceSetting(result);
		return result;
	}

	private void AudioSourceSetting(AudioSource audioSource)
	{
		audioSource.playOnAwake = false;
		audioSource.loop = false;
	}

	private IEnumerator StopPlaying(AudioSource audioSource, float time, GameplaySoundType soundType)
	{
		yield return new WaitForSeconds(time);
		audioSource.Stop();
		switch (soundType)
		{
		case GameplaySoundType.DigSound:
			_currentDigSounds--;
			break;
		case GameplaySoundType.Scratch:
			_currentScratchSounds--;
			break;
		case GameplaySoundType.Coin:
			_currentCoinSounds--;
			break;
		}
	}

	private void DigSound()
	{
		_currentDigSounds++;
		_digSoundTimer = 0.05f;
		AudioSource audioSource = this.audioSource();
		audioSource.volume = 0.5f;
		audioSource.clip = _digSounds[Random.Range(0, _digSounds.Length)];
		audioSource.pitch = Random.Range(1f, 1.3f);
		audioSource.Play();
		StartCoroutine(StopPlaying(audioSource, audioSource.clip.length, GameplaySoundType.DigSound));
	}

	private void ScratchSound()
	{
		_currentScratchSounds++;
		_scratchSoundTimer = 0.1f;
		AudioSource audioSource = this.audioSource();
		audioSource.volume = 0.5f;
		audioSource.clip = _scratchSounds[Random.Range(0, _scratchSounds.Length)];
		audioSource.pitch = Random.Range(1f, 1.5f);
		audioSource.Play();
		StartCoroutine(StopPlaying(audioSource, audioSource.clip.length, GameplaySoundType.Scratch));
	}

	public void CoinCollectedSound()
	{
		_currentCoinSounds++;
		_coinTimer = 0.05f;
		AudioSource audioSource = this.audioSource();
		audioSource.volume = 0.5f;
		audioSource.clip = _coinCollect;
		audioSource.pitch = 1f;
		audioSource.Play();
		StartCoroutine(StopPlaying(audioSource, audioSource.clip.length, GameplaySoundType.Coin));
	}

	private void UpgradeShownSound()
	{
		AudioSource audioSource = this.audioSource();
		audioSource.volume = 0.5f;
		audioSource.clip = _coinCollect;
		audioSource.pitch = 1f;
		audioSource.Play();
		StartCoroutine(StopPlaying(audioSource, audioSource.clip.length, GameplaySoundType.Upgrade));
	}

	public void SoundOff()
	{
		_canPlay = false;
		_machineAudioSource.Stop();
	}

	public void SoundOn()
	{
		_canPlay = true;
		_machineAudioSource.Play();
		_machineAudioSource.pitch = 0f;
	}

	internal void PlaySound(AudioClip clip)
	{
		AudioSource audioSource = this.audioSource();
		audioSource.volume = 0.5f;
		audioSource.clip = clip;
		audioSource.pitch = Random.Range(1f, 1.5f);
		audioSource.Play();
		StartCoroutine(StopPlaying(audioSource, audioSource.clip.length, GameplaySoundType.Custom));
	}
}
